package osTest;

import os.MiniSemaphore;

public class MiniSemaphoreTest
{
	private static Runnable runMethod;

	public static void main(String[] args) throws InterruptedException
	{
		final MiniSemaphore sem = new MiniSemaphore(1);
		runMethod = new Runnable()
		{
			@Override
			public void run()
			{
				while (true)
				{
					System.err.println(Thread.currentThread().getName() + " wants to enter.");
					// System.out.flush();
					sem.P();
					try
					{
						System.err.println(Thread.currentThread().getName() + " entered., c = " + sem.c);
						Thread.sleep(1000);
						System.err.println(Thread.currentThread().getName() + " left., c = " + sem.c);
					} catch (InterruptedException e)
					{
						e.printStackTrace();
					}
					sem.V();
					System.err.println(Thread.currentThread().getName() + " signaled, c = " + sem.c);
					// System.err.flush();
				}
			}
		};
		// final MiniMonitor m = new MiniMonitor(); // for testing the monitor
		/* for testing the monitor
		Runnable runMethod2 = new Runnable()
		{
			@Override
			public void run()
			{
				m.enter();
				System.out.println(Thread.currentThread().getName() + " entered.");
				try
				{
					Thread.sleep(1000);
				} catch (InterruptedException e)
				{
					e.printStackTrace();
				}
				m.leave();
				System.out.println(Thread.currentThread().getName() + " left.");
				
			}
		};
		*/
		Thread t1 = new Thread(runMethod, "Thread1");
		Thread t2 = new Thread(runMethod, "Thread2");
		Thread t3 = new Thread(runMethod, "Thread3");
		Thread t4 = new Thread(runMethod, "Thread4");
		t1.start();
		Thread.sleep(100);
		t2.start();
		Thread.sleep(100);
		t3.start();
		Thread.sleep(100);
		t4.start();
	}
}
